package vip.specimen.system.datasource.validation.implement;

import vip.specimen.system.datasource.entity.SystemArticleEntity;
import vip.specimen.system.datasource.validation.annotation.TitleUnique;
import vip.specimen.system.service.SystemArticleService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.awt.datatransfer.DataFlavor;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/12 16:04
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
public class TitleUniqueImp implements ConstraintValidator<TitleUnique, String> {

    @Resource
    private SystemArticleService articleService;
    @Resource
    private HttpServletRequest request;

    @Override
    public boolean isValid(String title, ConstraintValidatorContext constraintValidatorContext) {
        if (null == title) return true;
        SystemArticleEntity articleEntity = this.articleService.findByTitle(title);
        return (null == articleEntity || articleEntity.getUuid().toString().equals(request.getParameter("uuid")));
    }
}
